package com.jianyun.jyzs.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;


import com.jianyun.jyzs.R;

import java.util.List;

public class RadioDialogAdapter extends BaseAdapter {  
  
	private LayoutInflater layoutInflater;
    private boolean[] isCheckedArray;
    private List<String[]> dataList;

    public RadioDialogAdapter(Context context, List<String[]> dataList) {
        layoutInflater = LayoutInflater.from(context);
        this.dataList = dataList;
        isCheckedArray = new boolean[dataList.size()];
    }
    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int position, View convertView,ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.item_dialog_checkbox, null);

        TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        tv_name.setText(dataList.get(position)[1]);
        
        // 选择框checkbox
        final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
        checkBox.setButtonDrawable(R.drawable.check_blue);

        if (checkBox != null) {
            checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    // 群组中原来的成员一直设为选中状态
                    isCheckedArray[position] = isChecked;
                    // 如果是单选模式
                    if (isChecked) {
                        for (int i = 0; i < isCheckedArray.length; i++) {
                            if (i != position) {
                                isCheckedArray[i] = false;
                            }
                        }
                        notifyDataSetChanged();
                    } else {
                    	for (int i = 0; i < isCheckedArray.length; i++) {
                            isCheckedArray[i] = false;
                        }
                        notifyDataSetChanged();
                    }
                }
            });
            // 群组中原来的成员一直设为选中状态
            checkBox.setChecked(isCheckedArray[position]);
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public String getItem(int position) {
        if (position < 0) {
            return "";
        }
        return dataList.get(position)[1];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public int getItemId() {
    	for (int i = 0; i < isCheckedArray.length; i++) {
            if(isCheckedArray[i] == true){
            	return i;
            }
        }
    	return -1;
    }
}